import express from 'express';
import sql from '../../sql.js';
const router = express.Router();
// 获取各个表数据
router.get('/getTableCount', async (req, res) => {
  const tableNames = [
      'user', 'category', 'file', 'article', 'praise', 'comment', 'label', 'diary', 'message', 'record', 'menu', 'share'
  ];
  const counts = {};
  let fileTotalSize = 0;
  try {
      for (const tableName of tableNames) {
          const countQuery = `SELECT COUNT(*) AS total FROM ${tableName}`;
          const [result] = await sql.query(countQuery);
          counts[tableName] = result[0].total;
      }
      const countQuery = `SELECT COUNT(*) AS total FROM article WHERE type = 1`;
      const [result] = await sql.query(countQuery);
      counts['gallery'] = result[0].total;
      counts['article'] = counts['article'] - counts['gallery'];
      // 获取文件表总大小
      const fileSizeQuery = 'SELECT SUM(size) AS total_size FROM file';
      const [fileResult] = await sql.query(fileSizeQuery);
      if (fileResult[0].total_size) {
          fileTotalSize = fileResult[0].total_size;
      }
      res.send({
          status: 'success',
          message: '获取数据成功',
          data: {
            tableCounts: counts,
            fileTotalSize: fileTotalSize + 'MB'
          }
      });
  } catch (error) {
      console.error('获取表数据条数及文件总大小错误：', error);
      res.send({
          status: 'error',
          message: '服务器内部错误',
          errorCode: 500
      });
  }
});


export default router;
